# -*- coding: utf-8 -*-
import re
import urllib
from urllib import parse

import scrapy

from doubanbook.items import cctvItem


class CctvSpider(scrapy.Spider):
    name = 'cctv'
    allowed_domains = ['https://r.img.cctvpic.com']
    start_urls = ['https://r.img.cctvpic.com/so/cctv/list/9/201812/p44503_201812.js?_=1582796459139',
                  'https://r.img.cctvpic.com/so/cctv/list/9/201912/p44503_201912.js?_=1582796259139']
    custom_settings = {
        'ITEM_PIPELINES': {'doubanbook.pipelineCctv.cctvItemPipeline': 1,
                           'doubanbook.pipelineImage.cctvImagePipeline': 299,  # 激活图片下载管道
                           },

    }

    def parse(self, response):
        return self.encoder(response)
        # url='https://r.img.cctvpic.com/so/cctv/list/9/201912/p44503_201912.js?_=1582796259139'
        # yield Request(url=self.start_urls[0], dont_filter=True, callback=self.encoder)
        # yield Request(url=self.start_urls[1], dont_filter=True, callback=self.encoder)

    def encoder(self, response):
        print(response.encoding)
        # response.body.decode('cp1252')
        str = response.body
        print(str)
        print(type(str))
        datr = re.findall(r'eval[(]\'[(](.*?)[)]\'[)]\;', response.body_as_unicode())[0]
        # datr = re.findall(r'eval[(]\'[(](.*?)[)]\'[)]\;', str)[0]
        print(datr)
        import json
        datas = json.loads(datr)
        for data in datas:
            # print(data['detailsid'])
            # print(data['imagelink'])
            # print(data['playtime'])
            # print(data['duration'])
            # print(urllib.parse.unquote(data['title']))
            # print(re.findall(r'\d+',urllib.parse.unquote(data['title']).split()[1])[0])
            # print(data['targetpage'])

            shangping = cctvItem()
            shangping['location'] = urllib.parse.unquote(data['title']).split()[2]
            shangping['detailsid'] = data['detailsid']
            shangping['imagelink'] = data['imagelink']
            shangping['playtime'] = data['playtime']
            shangping['duration'] = data['duration']
            shangping['title'] = urllib.parse.unquote(data['title'])
            shangping['album_order_id'] = re.findall(r'\d+', urllib.parse.unquote(data['title']).split()[1])[
                0]  # data['album_order_id']
            shangping['targetpage'] = data['targetpage']
            yield shangping
